package net.dgg.zz.credential.queryCertificate.service;

import net.dgg.tmd.foundation.platform.common.util.TranslateUtil;
import net.dgg.tmd.foundation.platform.org.service.OrganizationManager;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.tmd.foundation.platform.user.service.UserService;
import net.dgg.zz.common.base.service.BaseService;
import net.dgg.zz.common.constant.CredentialConstant;
import net.dgg.zz.common.service.CommonService;
import net.dgg.zz.common.systemRecord.entity.SystemRecord;
import net.dgg.zz.common.systemRecord.service.SystemRecordService;
import net.dgg.zz.common.utils.StringUtils;
import net.dgg.zz.credential.baseCredential.dao.BaseCredentialDao;
import net.dgg.zz.credential.baseCredential.entity.BaseCredential;
import net.dgg.zz.credential.generalCredential.dao.GeneralCertificateDao;
import net.dgg.zz.credential.generalCredential.entity.GeneralCertificate;
import net.dgg.zz.credential.queryCertificate.dao.QueryCertificateDao;
import net.dgg.zz.credential.queryCertificate.dto.QueryCertificateDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zsx
 * @Title: QueryCertificateService
 * @ProjectName zz-sc-zs
 * @Description: 查询证书服务
 * @date 2018/8/28 14:06
 */
@Service
public class QueryCertificateService extends BaseService {
    @Autowired
    private QueryCertificateDao queryCertificateDao;
    @Resource
    private TranslateUtil translateUtil;
    @Autowired
    private CommonService commonService;
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private GeneralCertificateDao certificateDao;
    @Autowired
    private BaseCredentialDao baseCredentialDao;
    @Autowired
    private OrganizationManager organizationManager;
    @Autowired
    private UserService userService;
    @Autowired
    private SystemRecordService systemRecordService;


    /*
     * @Author zsx
     * @Date 2018-08-28 03:08:41
     * @Description  分页获取查询证书数据
     * @param params
     * @return List<QueryCertificateDto>
     */
    public List<QueryCertificateDto> selectCertificateByWithPage(Map<String, Object> params) {
        if (null == params.get("orderColumn")) {
            params.put("orderDir", "desc");
            params.put("orderColumn", "g.last_warehousing_time");
        }
        //0查询当前事业部  1查询自己的
        if (params.containsKey("isAll") && params.get("isAll").equals("0")) {
            params.put("signOrgId", commonService.findUserSyb(sessionManager.getCurrentUser()));
        } else if (params.containsKey("isAll") && params.get("isAll").equals("1")) {
            params.put("certificateUserId", sessionManager.getCurrentUser().getId());
        }
        params.put("libraryStatus", CredentialConstant.GENERAL_CREDENTIAL_LIBRARY_STATUS_OUT);
        params.put("certificateStatus", CredentialConstant.GENERAL_CREDENTIAL_STATUS_RETURN);
        params.put("generalType", CredentialConstant.GENERAL_CREDENTIAL);
        List<QueryCertificateDto> queryCertificates = queryCertificateDao.selectCertificateByWithPage(params);
        String[] column = {"certificateCategory", "certificateArea", "outgoingReason","certificateLevel","areaProvince"};
        return translateUtil.translateList(column, queryCertificates);
    }

    /*
     * @Author zsx
     * @Date 2018-08-28 05:08:39
     * @Description  查询需要导出的数据
     * @return List<QueryCertificateDto>
     */
    public List<QueryCertificateDto> selectExportCertificate(Map<String, Object> params) {
        //0查询当前事业部  1查询自己的
        if (params.containsKey("isAll") && params.get("isAll").equals("0")) {
            params.put("signOrgId", commonService.findUserSyb(sessionManager.getCurrentUser()));
        } else if (params.containsKey("isAll") && params.get("isAll").equals("1")) {
            params.put("certificateUserId", sessionManager.getCurrentUser().getId());
        }
        params.put("libraryStatus", CredentialConstant.GENERAL_CREDENTIAL_LIBRARY_STATUS_OUT);
        params.put("certificateStatus", CredentialConstant.GENERAL_CREDENTIAL_STATUS_RETURN);
        params.put("generalType", CredentialConstant.GENERAL_CREDENTIAL);
        List<QueryCertificateDto> queryCertificates = queryCertificateDao.selectExportCertificate(params);
        String[] column = {"certificateCategory", "certificateArea","areaProvince","outgoingReason","certificateLevel"};
        //日志
        SystemRecord record = new SystemRecord();
        record.setSource("1");
        record.setOperateType("53");
        record.setWorkTableName("zz_base_credential");
        record.setRemarks("导出查询证书");
        systemRecordService.save(record);
        return translateUtil.translateList(column, queryCertificates);
    }


    /*
     * @Author zsx
     * @Date 2018-08-29 09:08:12
     * @Description
     * @param remark 备注
     * @param referredPeople 转交人
     * @param baseId 证书id
     * @return DggRestResponse
     */
    @Transactional
    public Map<String, Object> transferCertificate(Long referredPeopleId, Long baseId, String remark) {
        Map<String, Object> map = new HashMap<>();
        map.put("status", true);
        UserEntity user = sessionManager.getCurrentUser();
        if (referredPeopleId.equals(user.getId())) {
            map.put("status", false);
            map.put("msg", "不能移交给自己");
            return map;
        }
        UserEntity referredPeople = userService.findUserById(referredPeopleId);
        if (null == referredPeople) {
            map.put("status", false);
            map.put("msg", "转交人不存在");
            return map;
        }
        //修改证书基本表
        BaseCredential credential = new BaseCredential();
        credential.setId(baseId);
        if (StringUtils.isNotEmpty(remark)) {
            credential.setRemarks(remark);
        }
        this.updateOpt(credential, user);
        baseCredentialDao.updateByPrimaryKeySelective(credential);
        //普通证书表
        GeneralCertificate certificate = new GeneralCertificate();
        certificate.setBaseId(baseId);
        certificate.setCertificateUserId(referredPeople.getId());
        certificate.setCertificateUserName(referredPeople.getRealName() + referredPeople.getLoginName());
        certificate.setCertificateUserOrgId(referredPeople.getOrgId());
        certificateDao.updateByPrimaryBaseId(certificate);
        //保存日志
        SystemRecord systemRecord = new SystemRecord();
        systemRecord.setSource("1");
        systemRecord.setOperateType("49");
        systemRecord.setWorkTableName("zz_base_credential，zz_general_certificate");
        systemRecord.setWorkId(baseId);
        systemRecord.setCertificateName(certificate.getCertificateName());
        systemRecord.setCertificateCode(certificate.getCertificateNo());
        systemRecord.setWorkNo(certificate.getCertificateNo());
        systemRecord.setSignOrgId(certificate.getSignOrgId());
        systemRecord.setSignOrgName(certificate.getSignOrgName());
        systemRecord.setCertificateType(certificate.getCertificateType());
        systemRecord.setRemarks(remark);
        systemRecordService.save(systemRecord);
        return map;
    }


}
